/**

This file is part of MaCI/GIMnet.

MaCI/GIMnet is free software: you can redistribute it and/or modify it 
under the terms of the GNU Lesser General Public License as published 
by the Free Software Foundation, either version 3 of the License, or 
(at your option) any later version.

MaCI/GIMnet is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with GIMnet. (See COPYING.LESSER) If not, see 
<http://www.gnu.org/licenses/>.

**/
/**
 * \file
 * \brief Very simple test/example for CSettings class.
 * \author Antti Maula <antti.maula@tkk.fi>
 *
 * $Id: settings_example.cpp,v 1.10 2009-05-13 07:18:25 amaula Exp $
 *
 */
#include "owndebug.h"
#include "settings.hpp"
#include <string>
#include <assert.h>

static std::string settingsfile = "settings_example.xml";


void testGroupFormat() 
{
  // Create settings instance
  gim::CSettings settings;
  bool res;

  // Attempt to read in settings XML file.
  if (settings.ReadSettingsXMLFile(settingsfile) == false) {
    dPrint(1, "Failed to read settings file '%s'. Abort.", settingsfile.c_str());

  } else {
    dPrint(1, "Successfully read in settings file '%s'.", settingsfile.c_str());
    std::string stringi;
    int integer_value;
    double double_value;


    // Get string for key 'teksti' (From zero level)
    res = settings.GetValue("teksti", stringi);
    dPrint(1, "GetValue(\"teksti\", ...) \t\t\treturned '%s' yielding 'stringi == '%s'",
	   res ? "Success" : "Failure", stringi.c_str());

    // Get string for key 'level1.teksti' (From first level)
    res = settings.GetValue("level1.teksti", stringi);
    dPrint(1, "GetValue(\"level1.teksti\", ...) \t\treturned '%s' yielding 'stringi == '%s'",
	   res ? "Success" : "Failure", stringi.c_str());

    // Get string for key 'level1.level2.teksti' (From second level)
    res = settings.GetValue("level1.level2.teksti", stringi);
    dPrint(1, "GetValue(\"level1.level2.teksti\", ...) \treturned '%s' yielding 'stringi == '%s'",
	   res ? "Success" : "Failure", stringi.c_str());

    // Get integer for key 'level1.level2.luku' (From second level)
    res = settings.GetValue("level1.level2.luku", integer_value);
    dPrint(1, "GetValue(\"level1.level2.luku\", ...) \t\treturned '%s' yielding 'integer_value == '%d'",
	   res ? "Success" : "Failure", integer_value);

    // Get double for key 'level1.level2.liukuluku' (From second level)
    res = settings.GetValue("level1.level2.liukuluku", double_value);
    dPrint(1, "GetValue(\"level1.level2.liukuluku\", ...) \treturned '%s' yielding 'double_value == '%.30lf'",
	   res ? "Success" : "Failure", double_value);

    // Get double for key 'level1.level2.liukuluku' (From second level, using a BaseKey)
    res = settings.SetBaseKey("level1.level2");
    res = settings.GetValue("liukuluku", double_value);
    dPrint(1, "BaseKey='level1.level2', GetValue(\"liukuluku\", ...) \treturned '%s' yielding 'double_value == '%.30lf'",
	   res ? "Success" : "Failure", double_value);
  }
}

void testSettingsFormat()
{
  gim::CSettings s;
  std::string keys[3];
  keys[0] = "Foo.bar.larp0";
  keys[1] = "Foo.bar.larp1";
  keys[2] = "Foo.bar.larp2";
  
  // Set & Write
  s.SetValue(keys[0], "foo");
  s.SetValue(keys[1], "foo");
  s.SetValue(keys[2], "foo");
  s.SetValue(keys[2], "foo", false);
  s.SetValue(keys[2], "foo", false);
  s.SetValue(keys[2], "foo", false);
  assert ( s.WriteSettingsXMLFile("new"+settingsfile) == true );
  
  // Read & Get
  gim::CSettings s2;
  s2.ReadSettingsXMLFile("new"+settingsfile);
  
  std::string v;
  std::vector<std::string> va;
  assert(s2.GetValue(keys[0], v) == true); va.push_back(v);
  assert(s2.GetValue(keys[1], v) == true); va.push_back(v);
  assert(s2.GetValue(keys[2], v) == true); va.push_back(v);
  assert(s2.GetValue("", v) == true); va.push_back(v);
  assert(s2.GetValue("", v) == true); va.push_back(v);
  assert(s2.GetValue("", v) == true); va.push_back(v);

  dPrint(ODTEST,"Verifying added data.");
  // Check that read data is equal
  std::string compare("foo");
  for(EACH_IN_i(va)) {
    assert(compare == *i);
  }

}

int main(int argc, char *argv[])
{
  int result = 0;
  int mode = 1;

  // Init debug facility & set level to 10
  debugInit();
  debugSetGlobalDebugLvl(10);

  printf("Usage: %s [set | group]\n\n", argv[0]);

  // Override settings file.
  if (argc > 1) {
    if (std::string(argv[1]) == "group") {
      mode = 0;
    } else {
      mode = 1;
    }
  }

  if (mode == 0) {
    testGroupFormat();

  } else {
    testSettingsFormat();

  }

  // Exit.
  return result;
}

/******************************************************************************
 *
 * $Log: not supported by cvs2svn $
 * Revision 1.9  2009-03-10 10:34:16  amaula
 * Reading multiple identical elements implemented.
 *
 * Revision 1.8  2009-03-10 09:31:35  amaula
 * Added testSequence for new Settings format too.
 *
 * Revision 1.7  2007-03-13 10:31:49  amaula
 * Added very simple example of using the BaseKey
 *
 * Revision 1.6  2007/03/02 13:35:45  amaula
 * One typo fixed.
 *
 * Revision 1.5  2007/03/02 13:28:14  amaula
 * Some code comments added
 *
 * Revision 1.4  2007/03/02 13:24:12  amaula
 * First working version
 *
 *****************************************************************************/
